#include "Decoder.h"


Decoder::Decoder()
{
}


Decoder::~Decoder()
{
}


void Decoder::Decode(char* szZipFile,char* szDstFile)
{
	FileReader cFileReader(szZipFile,RD_ZIP);
	FileWriter cFileWriter(szDstFile,WT_DST);

	Tree cTree(&cFileReader);
	Node* pRoot=cTree.GetRoot();

	int iSymbol;
	int iSymbolsCount=cFileReader.SymbolsCount();

	for (int i=0; i<iSymbolsCount; i++)
	{
		iSymbol=DecodeSymbol(pRoot,&cFileReader);
		cFileWriter.AddSymbol(iSymbol);
	}
}


int Decoder::DecodeSymbol(Node* pNode,FileReader* pFileReader)
{
	Node* pChild0=pNode->Child0();
	Node* pChild1=pNode->Child1();
	if (pChild0!=NO_NODE && pChild1!=NO_NODE)
	{
		int iBit;
		pFileReader->GetBit(&iBit);
		return DecodeSymbol(iBit==0? pChild0:pChild1,pFileReader);
	}
	return pNode->Symbol();
}
